@namespace MudBlazor
@inherits MudBaseInput<T>
@typeparam T

<CascadingValue Name="Standalone" Value="false" IsFixed="true">
    <div class="mud-select mud-autocomplete">
        <MudInputControl Label="@Label" Variant="@Variant" HelperText="@HelperText" HelperTextOnFocus="@HelperTextOnFocus" FullWidth="@FullWidth" Margin="@Margin" Class="@Classname" Style="@Style"
                         Error="@Error" ErrorText="@ErrorText" Disabled="@Disabled" @onclick="@ToggleMenu" Required="@Required">
            <InputContent>
                <MudInput @ref="_elementReference" @key="_elementKey" InputType="InputType.Text"
                          Class="mud-select-input" Margin="@Margin"
                          Variant="@Variant" 
                          TextUpdateSuppression="@TextUpdateSuppression"
                          Value="@Text" DisableUnderLine="@DisableUnderLine"
                          Disabled="@Disabled" ReadOnly="@ReadOnly" Error="@Error"
                          OnAdornmentClick="@OnAdornmentClick" AdornmentIcon="@_currentIcon" Adornment="@Adornment" AdornmentColor="@AdornmentColor" IconSize="@IconSize" AdornmentText="@AdornmentText"
                          Clearable="@Clearable" OnClearButtonClick="@OnClearButtonClick"
                          @attributes="UserAttributes"
                          TextChanged="OnTextChanged" OnBlur="OnInputBlurred"
                          OnKeyDown="@this.OnInputKeyDown"
                          OnKeyUp="@this.OnInputKeyUp" autocomplete=@("mud-disabled-"+Guid.NewGuid()) KeyUpPreventDefault="KeyUpPreventDefault"
                          Placeholder="@Placeholder" Immediate="true"
                          InputMode="@InputMode" Pattern="@Pattern"
                          T="string" />
                <MudPopover Open="@IsOpen" MaxHeight="@MaxHeight" AnchorOrigin="@AnchorOrigin" TransformOrigin="@TransformOrigin" Class="@PopoverClass" RelativeWidth="true">
                    @if (_items != null && _items.Length != 0)
                    {
                        <MudList Clickable="true" Dense="@Dense">
                            @for (var index = 0; index < _items.Length; index++)
                            {
                                var item = _items[index];
                                bool is_selected = index == _selectedListItemIndex;
                                bool is_disabled = !_enabledItemIndices.Contains(index);
                                <MudListItem @key="@item" id="@GetListItemId(index)" Disabled="@(is_disabled)" OnClick="@(() => SelectOption(item))" Class="@(is_selected ? "mud-selected-item" : "")">
                                    @if (ItemTemplate == null)
                                    {
                                        @GetItemString(item)
                                    }
                                    else if (is_disabled && ItemDisabledTemplate != null)
                                    {
                                        @ItemDisabledTemplate(item)
                                    }
                                    else if (is_selected)
                                    {
                                        @if (ItemSelectedTemplate == null)
                                            @ItemTemplate(item)
                                        else
                                            @ItemSelectedTemplate(item)
                                    }
                                    else
                                    {
                                        @ItemTemplate(item)
                                    }
                                </MudListItem>
                            }
                        </MudList>
                    }
                </MudPopover>
            </InputContent>
        </MudInputControl>
    </div>
</CascadingValue>

<MudOverlay Visible="IsOpen" OnClick="@ToggleMenu" LockScroll="false" />